package com.youlu.campus.web.community.service.impl;

import com.mongodb.client.result.UpdateResult;
import com.youlu.campus.base.base.ContentTypeEnum;
import com.youlu.campus.base.base.InteractiveActionEnum;
import com.youlu.campus.base.community.ContentTopicBase;
import com.youlu.campus.service.base.BaseService;
import com.youlu.campus.web.community.dto.InteractiveMqRequest;
import com.youlu.campus.web.community.entity.ArticleInfo;
import com.youlu.campus.web.community.entity.CommContent;
import com.youlu.campus.web.community.entity.ContentComment;
import com.youlu.campus.web.community.entity.TopicInfo;
import com.youlu.campus.web.community.service.InteractiveMqService;
import com.youlu.campus.web.sign.entity.SignPlanDailyPO;
import com.youlu.campus.web.common.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 互动数据MQ消息处理接口实现类
 * @author: YangJ
 * @date: 2020/12/14 16:35
 */
@Slf4j
@Service("interactiveMqService")
public class InteractiveMqServiceImpl extends BaseService implements InteractiveMqService {


    @Override
    public void handleInteractive(InteractiveMqRequest request) {
        log.info("开始处理互动数据消息:{}", JsonUtil.toJSON(request));
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(request.getBid()));
        Update update = new Update();
        update.inc(request.getAction()+"Count", request.getValue());
        //判断业务类型
        if(ContentTypeEnum.DT.getCode().equals(request.getType())){
            //动态
            if(!request.getAction().equals(InteractiveActionEnum.HOT.getCode())){
                update.inc("hotCount", request.getValue());
            }
            UpdateResult result = mongoTemplate.updateFirst(query, update, CommContent.class);
            if(result.getModifiedCount() >0){
                log.info("动态互动信息维护成功,id:{}",request.getBid());
                //查询动态关联话题-维护话题互动数据
                CommContent content = mongoTemplate.findOne(query, CommContent.class);
                if(content.getLinked().equals(true)){
                    List<String> topicIds = content.getTopics().stream().map(ContentTopicBase::getTopicId)
                            .collect(Collectors.toList());
                    query = new Query();
                    query.addCriteria(Criteria.where("id").in(topicIds));
                    UpdateResult result1 = mongoTemplate.updateMulti(query, update, TopicInfo.class);
                    if(result1.getModifiedCount() >0){
                        log.info("动态关联话题信息信息维护成功,ids:{}",JsonUtil.toJSON(topicIds));
                    }
                }
            }
        }else if(ContentTypeEnum.WZ.getCode().equals(request.getType())){
            //文章
            if(!request.getAction().equals(InteractiveActionEnum.HOT.getCode())){
                update.inc("hotCount", request.getValue());
            }
            UpdateResult result = mongoTemplate.updateFirst(query, update, ArticleInfo.class);
            if(result.getModifiedCount() >0){
                log.info("文章互动信息维护成功,id:{}",request.getBid());
            }
        }else if(ContentTypeEnum.DK.getCode().equals(request.getType())){
            //打卡
            if(!request.getAction().equals(InteractiveActionEnum.HOT.getCode())){
                update.inc("hotCount", request.getValue());
            }
            UpdateResult result = mongoTemplate.updateFirst(query, update, SignPlanDailyPO.class);
            if(result.getModifiedCount() >0){
                log.info("打卡互动信息维护成功,id:{}",request.getBid());
            }
        }else if(ContentTypeEnum.PL.getCode().equals(request.getType())){
            //评论
            UpdateResult result = mongoTemplate.updateFirst(query, update, ContentComment.class);
            if(result.getModifiedCount() >0){
                log.info("评论互动信息维护成功,id:{}",request.getBid());
            }
        }else if(ContentTypeEnum.HT.getCode().equals(request.getType())){
            //话题
            UpdateResult result = mongoTemplate.updateFirst(query, update, TopicInfo.class);
            if(result.getModifiedCount() >0){
                log.info("话题统计信息维护成功,id:{}",request.getBid());
            }
        }else{
            log.error("消息类型有误！,{}",request.getType());
        }
    }
}
